/**
 * @{#} ImageLoaderView.java Create on 2014年11月13日 上午10:18:44    
 *
 * @author <a href="mailto:evan0502@qq.com">Evan</a>   
 * @version 1.0
 * @description
 *
 */
package com.common.widget;

import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;

import com.common.R;
import com.common.activity.ImageShowActivity;
import com.common.constant.Constants;
import com.common.manager.init.InitManager;
import com.common.utility.CommonUtility;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;


public class ImageLoaderView extends ImageView {


    /**
     * 是否显示为圆角图片
     */
    private boolean mIsRoundCorner;

    /**
     * 是否显示为圆形图片
     */
    private boolean mIsRound;

    private float mImageLoaderWidthHeightRate;

    // 是否点击跳转到大图预览
    private boolean mImageLoaderClick2preview;

    private String mUrl;

    private Context mContext;

    private Drawable mEmptyDrawable;

    private Drawable mLoadingDrawable;

    private Drawable mFailDrawable;

    /**
     * @return the mIsRoundCorner
     */
    public boolean ismIsRoundCorner() {
        return mIsRoundCorner;
    }

    /**
     * @param mIsRoundCorner
     *            the mIsRoundCorner to set
     */
    public void setmIsRoundCorner(boolean mIsRoundCorner) {
        this.mIsRoundCorner = mIsRoundCorner;
    }

    /**
     * @return the mIsRound
     */
    public boolean ismIsRound() {
        return mIsRound;
    }

    /**
     * @param mIsRound
     *            the mIsRound to set
     */
    public void setmIsRound(boolean mIsRound) {
        this.mIsRound = mIsRound;
    }

    /**
     * @return the mImageLoaderWidthHeightRate
     */
    public float getmImageLoaderWidthHeightRate() {
        return mImageLoaderWidthHeightRate;
    }

    /**
     * @param mImageLoaderWidthHeightRate
     *            the mImageLoaderWidthHeightRate to set
     */
    public void setmImageLoaderWidthHeightRate(float mImageLoaderWidthHeightRate) {
        this.mImageLoaderWidthHeightRate = mImageLoaderWidthHeightRate;
    }

    /**
     * @param context
     * @param attrs
     * @param defStyle
     */
    public ImageLoaderView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context, attrs);
    }

    /**
     * @param context
     * @param attrs
     */
    public ImageLoaderView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    /**
     * @param context
     */
    public ImageLoaderView(Context context) {
        super(context);
        init(context, null);
    }

    /*
     * (non-Javadoc)
     *
     * @see android.widget.ImageView#onMeasure(int, int)
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (mImageLoaderWidthHeightRate > 0) {
            int width = MeasureSpec.getSize(widthMeasureSpec);
            int height = MeasureSpec.getSize(heightMeasureSpec);
            height = (int) (width * mImageLoaderWidthHeightRate);
            setMeasuredDimension(width, height);
        }
    }

    /**
     * 初始化属性 method desc：
     *
     * @param attrs
     *            isRound isRoundCorner isRound为true时，isRoundCorner则无效
     */
    public void init(final Context context, AttributeSet attrs) {
        mContext = context;
        if (!CommonUtility.Utility.isNull(attrs)) {
            TypedArray attributes = context.obtainStyledAttributes(attrs,
                    R.styleable.ImageLoaderAttr, 0, 0);
            mIsRound = attributes.getBoolean(
                    R.styleable.ImageLoaderAttr_imageLoader_round, false);
            mIsRoundCorner = attributes.getBoolean(
                    R.styleable.ImageLoaderAttr_imageLoader_roundCorner, false);
            mImageLoaderWidthHeightRate = attributes.getFloat(
                    R.styleable.ImageLoaderAttr_imageLoader_width_height_rate,
                    0);
            mImageLoaderClick2preview = attributes.getBoolean(
                    R.styleable.ImageLoaderAttr_imageLoader_click2preview,
                    false);
            mEmptyDrawable = attributes.getDrawable(R.styleable.ImageLoaderAttr_imageLoader_empty);
            mLoadingDrawable = attributes.getDrawable(R.styleable.ImageLoaderAttr_imageLoader_loading);
            mFailDrawable = attributes.getDrawable(R.styleable.ImageLoaderAttr_imageLoader_fail);
            attributes.recycle();
        }

        if (mImageLoaderClick2preview) {
            setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                    Intent intent = new Intent(context, ImageShowActivity.class);
                    intent.putExtra(Constants.IDENTITY.IDENTITY_FILEPATH, mUrl);
                    context.startActivity(intent);
                }
            });
        }
    }

    /**
     * 根据uri地址加载图片，根据options进行压缩 method desc：
     *
     * @param url
     *            图片源地址
     * @param options
     *            图片压缩对象
     */
    public void loadImage(String url, DisplayImageOptions options) {
        mUrl = url;
        ImageLoader.getInstance().displayImage(url, this, options);
    }

    public void loadImage(String url,int loadImage,int loadingFail,int emptyUri){
        mUrl = url;
        DisplayImageOptions options = null;
        int roundSize = 0;
        if(mIsRound){
            roundSize = 100;
        }else if(mIsRoundCorner){
            roundSize = CommonUtility.UIUtility.dip2px(mContext, 10);
        }else{
            roundSize = 0;
        }

        DisplayImageOptions.Builder build = InitManager.getInstance().getOptionBuilder(roundSize);
        if(loadImage != 0){
            build.showImageOnLoading(loadImage);
        }

        if(loadingFail != 0){
            build.showImageOnFail(loadingFail);
        }

        if(emptyUri != 0){
            build.showImageForEmptyUri(emptyUri);
        }

        options = build.build();
        ImageLoader.getInstance().displayImage(url, this, options);
    }

    /**
     * 根据uri地址加载图片，无压缩 method desc：
     *
     * @param url
     *            图片源地址
     */
    public void loadImage(String url) {
        mUrl = url;
        DisplayImageOptions options = null;
        int roundSize = 0;
        if(mIsRound){
            roundSize = 100;
        }else if(mIsRoundCorner){
            roundSize = CommonUtility.UIUtility.dip2px(mContext, 10);
        }else{
            roundSize = 0;
        }

        DisplayImageOptions.Builder build = InitManager.getInstance().getOptionBuilder(roundSize);
        if(mEmptyDrawable != null){
            build.showImageForEmptyUri(mEmptyDrawable);
        }

        if(mLoadingDrawable != null){
            build.showImageOnLoading(mLoadingDrawable);
        }

        if(mFailDrawable != null){
            build.showImageOnFail(mFailDrawable);
        }

        options = build.build();

        ImageLoader.getInstance().displayImage(url, this, options);
    }


}
